package com.hy.demo.app;

import android.content.pm.ActivityInfo;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.lifecycle.Lifecycle;
import androidx.lifecycle.LifecycleEventObserver;
import androidx.lifecycle.LifecycleOwner;

import com.hy.demo.R;
import com.hy.frame.base.TemplateUIBuilder;
import com.hy.frame.common.ILoadingDialog;
import com.hy.frame.common.ILoadingUI;
import com.hy.frame.ui.LoadingDialog;
import com.hy.frame.ui.LoadingUI;
import com.hy.frame.util.LogUtil;

/**
 * title 无
 * author heyan
 * time 19-7-11 下午2:30
 * desc 无
 */
public abstract class BaseActivity extends com.hy.frame.base.BaseActivity implements TemplateUIBuilder.ITemplateListener {
    private TemplateUIBuilder mTemplate;
    private ILoadingDialog mLoadingDialog;
    private ILoadingUI mLoadingUI;

    @Override
    protected int getScreenOrientation() {
        return ActivityInfo.SCREEN_ORIENTATION_PORTRAIT;
    }

    protected boolean isSingleLayout() {
        return false;
    }

    protected int getLayoutId() {
        return 0;
    }

    @Override
    protected boolean isTranslucentStatus() {
        return true;
    }

    @Override
    protected boolean initAttrs() {
        return mTemplate == null;
    }

    @Override
    protected void initLayout() {
        if (mTemplate == null)
            mTemplate = new TemplateUIBuilder(getContext(), this)
                    .setCheckFastClick(true)
                    .setSingleLayout(isSingleLayout())
                    .setTranslucentStatus(isTranslucentStatus())
                    .setBaseLayout(R.layout.v_base)
                    .setLayoutView(getLayoutId()).build();
        getLifecycle().addObserver(new LifecycleEventObserver() {
            @Override
            public void onStateChanged(@NonNull LifecycleOwner source, @NonNull Lifecycle.Event event) {
                LogUtil.d(BaseActivity.this.getClass(), "event=" + event);
            }
        });
        setContentView(mTemplate.getLayoutView());
    }

    public TemplateUIBuilder getTemplate() {
        return this.mTemplate;
    }

    public ILoadingUI getLoadingUI() {
        if (mTemplate == null || mTemplate.getMainView() == null) return null;
        if (mLoadingUI == null) {
            mLoadingUI = new LoadingUI(mTemplate.getMainView());
        }
        return mLoadingUI;
    }

    public ILoadingDialog getLoadingDialog() {
        if (mLoadingDialog == null) {
            mLoadingDialog = new LoadingDialog(getContext());
        }
        return mLoadingDialog;
    }
    //    protected void initHeader(int drawLeft, int titleId, int strRightId) {
//        getTemplateUI().setHeaderLeft(drawLeft);
//        getTemplateUI().setTitle(titleId);
//        getTemplateUI().setHeaderRightTxt(getString(strRightId));
//    }
//
//    protected void initHeader(int drawLeft, int titleId) {
//        getTemplateUI().setHeaderLeft(drawLeft);
//        getTemplateUI().setTitle(titleId);
//    }


    @Override
    public void setTitle(CharSequence title) {
        if (mTemplate != null)
            mTemplate.setTitle(title);
        else
            super.setTitle(title);
    }

    protected void configBack() {
        getTemplate().setHeaderLeft(R.mipmap.ic_back);
    }

    @Override
    public void onLeftClick() {
        onBackPressed();
    }

    @Override
    public void onRightClick() {

    }

    @Override
    public void onLoadViewClick() {

    }

    @Override
    public void onViewClick(View v) {

    }
}
